import algorithm.SimulatedAnneling;
import entity.item;
import entity.taskInfo;
import entity.worker;
import javafx.concurrent.Worker;
import pojo.TaskAssignment;
import pojo.WorkerHandle;
import utils.*;

import java.time.LocalDate;
import java.util.*;

public class main {
    public static void main(String[] args) {
        List<item> items=ExcelReader.readPartRecord(".\\src\\main\\resources\\resource\\物料数据3.xlsx");

        // 整年任务数据读入
        List<taskInfo> taskInfos = new ArrayList<>();
        taskInfos.addAll(ExcelReader.readTaskInfo(".\\src\\main\\resources\\resource\\1-3月任务.xlsx"));
        taskInfos.addAll(ExcelReader.readTaskInfo(".\\src\\main\\resources\\resource\\4-6月任务.xlsx"));
        taskInfos.addAll(ExcelReader.readTaskInfo(".\\src\\main\\resources\\resource\\7-8月任务.xlsx"));
        taskInfos.addAll(ExcelReader.readTaskInfo(".\\src\\main\\resources\\resource\\9-11月任务.xlsx"));
        taskInfos.addAll(ExcelReader.readTaskInfo(".\\src\\main\\resources\\resource\\11-12月任务.xlsx"));
        //taskInfos.addAll(ExcelReader.readTaskInfoActually("D:\\idea\\HFHKGY\\src\\main\\resources\\resource\\全年移交数据.xlsx"));
        //taskInfos.addAll(ExcelReader.readTaskInfoActually("D:\\idea\\HFHKGY\\src\\main\\resources\\resource\\新建 Microsoft Excel 工作表.xlsx"));
        int number=0;
        int time=0;
        for (int i = 0; i < taskInfos.size(); i++) {
            number+=taskInfos.get(i).getTaskCount();
           // time+=taskInfos.get(i).getTaskCount()*taskInfos.get(i).get
        }
        System.out.println("加工零件数量为"+number);
        List<worker> workers=ExcelReader.readEmployeeRecord(".\\src\\main\\resources\\resource\\人员数据.xlsx");
        // 数据过滤
        DataFilter dataFilter=new DataFilter();
        List<item> vaildItem=dataFilter.filterItems(items);
        System.out.println("vaildItem = " + vaildItem.size());
        List<worker> vaildWorker=dataFilter.filterWorkers(workers);
        System.out.println("vaildWorker = " + vaildWorker.size());
        List<taskInfo> vaildTaskInfo=dataFilter.filterTaskInfos(taskInfos,vaildItem);
        System.out.println("vaildTaskInfo = " + vaildTaskInfo.size());
        // 初始化月度需求和产能 (先初始化为空，后续由HandleTaskflow加载)
        Map<Integer, List<TaskAssignment>> monthlyDemands = new HashMap<>();
        Map<Integer, Double> workcenterCapacities = new HashMap<>();

        // 创建HandleTaskflow实例
        HandleTaskflow taskflow = new HandleTaskflow(
                workcenterCapacities,
                monthlyDemands,
                "钣金一工段",
                2024,
                vaildItem,
                vaildTaskInfo,
                vaildWorker
        );
        taskflow.initializeAllMonths(1, 12, 8);
//        taskflow.processTasks(1, 12);
//        Map<Integer, List<TaskAssignment>> finalTasks = taskflow.getFinalTasks();
//        List<TaskAssignment> outsourcedTasks = taskflow.getOutsourcedTasks();
//        printResults(finalTasks, outsourcedTasks);


//        List<WorkerHandle> worker_1=new ArrayList<>();
//        for (int i = 0; i <vaildWorker.size(); i++) {
//                worker_1.add(new WorkerHandle(vaildWorker.get(i)));
//        }
//        int num=0;
//        for(TaskAssignment taskAssignment:finalTasks.get(1)){
//            num+=taskAssignment.getAssignedQuantity();
//        }
         //System.out.println("要分配"+num+"个零件");
        //SimulatedAnneling simulatedAnneling=new SimulatedAnneling(finalTasks.get(1),1,2025,worker_1);
        //simulatedAnneling.run();
    }
    private static void printResults(Map<Integer, List<TaskAssignment>> finalTasks,
                                     List<TaskAssignment> outsourcedTasks) {
        // 打印最终任务分配
        System.out.println("\n===== 最终任务分配结果 =====");
        int totalAssignedTasks = 0;
        double totalAssignedWorkload = 0;

        List<Integer> months = new ArrayList<>(finalTasks.keySet());
        Collections.sort(months);

        for (int month : months) {
            List<TaskAssignment> tasks = finalTasks.get(month);
            double monthWorkload = 0;
            for (TaskAssignment task : tasks) {
                monthWorkload += task.getPreCycle()+task.getMakeCycle() * task.getAssignedQuantity();
            }

            System.out.printf("第%d月: %d项任务, 总工时: %.2f小时\n",
                    month, tasks.size(), monthWorkload);

            totalAssignedTasks += tasks.size();
            totalAssignedWorkload += monthWorkload;
        }

        // 打印外包任务
        System.out.println("\n===== 外包任务 =====");
        double outsourcedWorkload = 0;
        for (TaskAssignment task : outsourcedTasks) {
            outsourcedWorkload += task.getPreCycle()+task.getMakeCycle() * task.getAssignedQuantity();
        }

        System.out.printf("外包任务数: %d, 总工时: %.2f小时\n",
                outsourcedTasks.size(), outsourcedWorkload);

        // 打印总结
        System.out.println("\n===== 总结 =====");
        System.out.printf("总分配任务数: %d\n", totalAssignedTasks);
        System.out.printf("总分配工时: %.2f小时\n", totalAssignedWorkload);
        System.out.printf("总外包工时: %.2f小时\n", outsourcedWorkload);
        System.out.printf("总工时: %.2f小时\n", totalAssignedWorkload + outsourcedWorkload);
    }
}
